package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import aym.util.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import striveen.util.used.view.view.HorizontalListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/6 0006.
 * 商城主题
 */
public class ShopMallThemeAdapter extends MyBaseAdapter {

	/**
	 * 按钮和推荐的列表的点击事件
	 */
	private ItemClickListener itemClickListener;

	public ItemClickListener getItemClickListener() {
		return itemClickListener;
	}

	public void setItemClickListener(ItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}

	public ShopMallThemeAdapter(Context context) {
		super(context);
	}

    public ShopMallThemeAdapter(Context context, List<? extends Map<String, ?>> datas) {
        super(context, datas);
    }

    @Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ShopMallAdapterViewHolder shopMallAdapterViewHolder;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.shopmall_theme_item, null);
            shopMallAdapterViewHolder = new ShopMallAdapterViewHolder();
			ViewUtils.inject(shopMallAdapterViewHolder, view);
			view.setTag(shopMallAdapterViewHolder);
		} else {
            shopMallAdapterViewHolder = (ShopMallAdapterViewHolder) view.getTag();
		}
		setItemTag(i,shopMallAdapterViewHolder);
		bindData(i, shopMallAdapterViewHolder);
		return view;
	}

	private void setItemTag(int i, ShopMallAdapterViewHolder shopMallAdapterViewHolder) {
        shopMallAdapterViewHolder.collect_iv.setTag(i);
        shopMallAdapterViewHolder.share_iv.setTag(i);
	}

	private void bindData(final int i, ShopMallAdapterViewHolder viewHolder) {
		JsonMap jsonMap= (JsonMap) datas.get(i);
		viewHolder.shopmall_theme_item_title_tv.setText(jsonMap.getStringNoNull("Title"));
		String picPath=jsonMap.getStringNoNull("MainImage");
		if(MyApplication.getInstance().getBitmapHashMap().containsKey(picPath)) {
			viewHolder.big_iv.setImageBitmap(MyApplication.getInstance().getBitmapHashMap().get(picPath));
		} else {
			ImageLoader.getInstance().displayImage(picPath,viewHolder.big_iv,MyApplication.getInstance().getDefaultOption(),new ImageLoaderBitmapCallBack());
		}
	}


	public class ShopMallAdapterViewHolder {
		@ViewInject(R.id.big_iv)
		public ImageView big_iv;

		@ViewInject(R.id.shopmall_theme_item_title_tv)
		public TextView shopmall_theme_item_title_tv;

		@ViewInject(R.id.collect_iv)
		public ImageView collect_iv;

        @ViewInject(R.id.share_iv)
        public ImageView share_iv;


		@OnClick(R.id.share_iv)
		public void share_iv_tv(View view) {
			if (itemClickListener != null) {
				itemClickListener.onItemClickListener(Constant.SHARE_IV_CLICK, (Integer) view.getTag(), -1);
			}
		}

        @OnClick(R.id.collect_iv)
        public void collect_iv_tv(View view) {
            if (itemClickListener != null) {
                itemClickListener.onItemClickListener(Constant.COLLECT_IV_CLICK, (Integer) view.getTag(), -1);
            }
        }

	}

	public interface ItemClickListener {
		/**
		 * 返回item的空间的点击事件
		 *
		 * @param itemCompontType          点击事件的类型 RECOMMAND_ITEM_CLICK：推荐列表的点击事件  MORE_CLICK：更多点击
		 * @param position      点击的item的位置
		 * @param childPosition 当点击的是推荐列表的item 返回数据的子list的位置 否则返回-1
		 */
		public void onItemClickListener(int itemCompontType, int position, int childPosition);
	}
}
